package 字符串的遍历;

import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        String str="hello world";

        //1、for循环
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            System.out.print(c);
        }

        //2、foreach循环
        /*
            public char[] toCharArray()
            将字符串转换为字符数组
         */
        for (char c:str.toCharArray()) {
            System.out.print(c);
        }

        //3、迭代器
        Iterator<Character> iterator = str.chars().mapToObj(c -> (char) c).iterator();
        while (iterator.hasNext()) {
            char c = iterator.next();
            System.out.print(c);
        }

        //4、Java 8 中的流式API遍历
        str.chars().mapToObj(c -> (char) c).forEach(c -> {
            // 进行相应的操作
        });
    }
}
